package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.query.EmployeeQueryObject;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) throws  Exception{
        model.addAttribute("result", employeeService.query(qo));
        model.addAttribute("depts", departmentService.list());
        return "employee/list";
    }
    @RequestMapping("/input")
    public String input(Long id, Model model) throws  Exception{
        //点击添加或者编辑后跳转到input.jsp页面
        /*
        根据是否携带id判断是否修改或者添加
        如果是修改咋修要将该页面所需的所有的信息查询出来,再放进去
        employeeService.get(id);回显根据id查询出该员工所有的信息
        departmentService.list();查询出左右的部门信息回显部门
        roleService.list();查询出所有的角色信息回显到角色里面
         */
        if(id !=null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("entity", employee);
        }
            List<Department> list = departmentService.list();
            List<Role> roles = roleService.list();
            model.addAttribute("depts",list);
            model.addAttribute("roles",roles);
        return "employee/input";
     /*   if(id != null){
            model.addAttribute("entity", employeeService.get(id));
        }
        model.addAttribute("depts", departmentService.list());
        model.addAttribute("roles", roleService.list());
        return "employee/input";*/
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee entity, Long[] ids,Model model) throws  Exception{
        if(entity.getId() != null){
            employeeService.update(entity, ids);
        }else{
            employeeService.save(entity, ids);
        }
        return "redirect:/employee/list";
    }

    @RequestMapping("/delete")
    public Object delete(Long id) throws  Exception{
        JsonResult jsonResult = new JsonResult();
        try {
            if(id != null){
                employeeService.delete(id);
            }
        }catch (Exception e){
            jsonResult.mark("-----");
        }
        return jsonResult;
    }


}
